var express = require('express');
var router = express.Router();
var Articles = require('../models/articles');

/* POST users listing. */
router.post('/', function(req, res, next) {
      // 获取用户提交的信息
    var postData = {
        title: req.body.title,
        author: req.session.sign.username,//从session里拿用户名
        authorId:req.session.sign.userId,
        content: req.body.content,
    };
    console.log(req.session.sign)
    //保存文章信息，到数据库
    Articles.create(postData, function (err, data) {
        if (err) {
            res.send({
                state:0,
                msg:"保存失败"  
            })  
            throw err;
        }else{
            console.log('保存成功');
            res.send({
                state:1,
                msg:"保存成功"  
            })  
        }
    })

});

/* GET users listing. */
router.get('/', function(req, res, next) {
		var ArticleList = Articles.find({}, function (err, data) {
        if (err) throw  err;
        res.send(data)
    });
})
module.exports = router;
